package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {

    @Override
    public void importOrderSettings(List<String[]> orderSettingList) {
        //   根据 日期 判断 ： 如果数据库表 已经存在 日期  ---天  批量 update    最大预约
        //    如果数据库表  没有指定的日期记录     批量 insert

        List<OrderSetting> orderSettingImportList = changeStringArrayToList(orderSettingList);
        //  判断 数据库 是否已经存在 对应日期数据
        //     根据 主键判断  save  还是 update
        //  orderSettingImportList insert  update  (OrderSetting  id)
        for (OrderSetting orderSetting : orderSettingImportList) {
            //  按照日期查询  是否存在   存在  orderSetting  set id
            QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<OrderSetting>();
            queryWrapper.eq("orderdate", orderSetting.getOrderDate()); //  where  orderdate = '2020-11-9'
            OrderSetting orderSettingIsOrNotExist = baseMapper.selectOne(queryWrapper);
            if (orderSettingIsOrNotExist != null) {
                //  update
                orderSetting.setId(orderSettingIsOrNotExist.getId());
            }
        }
        saveOrUpdateBatch(orderSettingImportList);  //   批量更新和 添加操作
    }

    @Override
    public Map findSettingData(int year, int month) {

        String beginDate = year+"-"+month+"-1";
        String endDate = year+"-"+month+"-31";
        List<OrderSetting> orderSettings = baseMapper.findSettingDataByYearAndMonth(beginDate,endDate);
        //  Map   前后数据交互 格式 对象
        Map<String,Object> map  = new HashMap();
        for (OrderSetting orderSetting : orderSettings) {
            Date orderDate = orderSetting.getOrderDate();
            String dateString = DateUtils.parseDate2String(orderDate, "yyyy-MM-dd");
            //   需要的 数据 传递 前端   灵活 方便    List  Map    json
            Map  orderSettingMap = new HashMap();
            orderSettingMap.put("number",orderSetting.getNumber());
            orderSettingMap.put("reservations",orderSetting.getReservations());
            map.put(dateString,orderSettingMap);
        }
        return map;
    }

    @Override
    public Boolean updateNumberByOrderdate(int number, String orderdate) {
        baseMapper.updateNumberByOrderdate(number,orderdate);
        return true;
    }

    private List<OrderSetting> changeStringArrayToList(List<String[]> orderSettingList) {
        if (orderSettingList != null && orderSettingList.size() != 0) {
            List<OrderSetting> orderSettings = new ArrayList<OrderSetting>();
            for (String[] strings : orderSettingList) {
                OrderSetting orderSetting = new OrderSetting();
                //单元格 行为单位 第一行第一列单元格下标为0 第一行第二列为1... 对应着数据库中的t_ordersetting
                orderSetting.setReservations(0);
                orderSetting.setNumber(Integer.parseInt(strings[1]));
                orderSetting.setOrderDate(DateUtils.parseString2Date(strings[0], "yyyy/MM/dd"));
                orderSettings.add(orderSetting);
            }
            return orderSettings;
        }
        return null;
    }
}
